package servlet.miniprojet.bdd;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionBDD {

	
	//private static ConnectionBDD instance=null;
	private static Connection connect;
	
	private ConnectionBDD(){
		System.out.println("BDD");
		try {
			Class.forName(ConfigBDD.pilote);
			connect = DriverManager.getConnection(ConfigBDD.basedb,ConfigBDD.userdb,ConfigBDD.pwddb);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public int exec(String req) {
		Statement  st;
		int result=0;
		try {
			st = connect.createStatement();
			result = st.executeUpdate(req);
			st.close();

		}catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	public int update(String req) {
		PreparedStatement st;
		int result=0;
		try {
			st = connect.prepareStatement(req);
			result = st.executeUpdate();	 
			st.close();

		}catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	

	
	
	public ResultSet query(String req) {
		Statement instruction;
		ResultSet resultat = null;
		try {
			instruction = connect.createStatement();
			resultat = instruction.executeQuery(req);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return resultat;
	}

	
	public static Connection getInstance(){
		if(connect == null){
			new ConnectionBDD();
		}
		return connect;
	}
	
}


